/*
 * Copyright (C) 2019 ~ 2022 Uniontech Technology Co., Ltd.
 *
 * Author:     zhangbingyi <zhangbingyi@uniontech.com>
 *
 * Maintainer: zhangbingyi <zhangbingyi@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "peripheralsmanagementwidget.h"
#include "peripheralsmanagementmodel.h"
#include "peripheralsmanageopendialog.h"

#include <QLabel>
#include <QVBoxLayout>
#include <QSpacerItem>

PeripheralsManagementWidget::PeripheralsManagementWidget(PeripheralsManagementModel *model, QFrame *parent)
    : QFrame(parent)
    , m_model(model)
    , m_USBSwitchItem(nullptr)
    , m_cameraSwitchItem(nullptr)
    , m_printerSwitchItem(nullptr)
{
    // 初始化
    initUI();

    if (m_model->getPeripheralsKey(PERIPHERALS_MANAGE_USB_STATUS)) {
        m_cameraSwitchItem->setVisible(false);
        m_printerSwitchItem->setVisible(false);
    }

    connect(m_model, SIGNAL(notifyPeripheralsRevert()), SLOT(notifyPeripheralsRevert()));

    connect(m_model, SIGNAL(synPeripheralsStatus(const QString &, bool)), this, SLOT(synPeripheralsStatus(const QString &, bool)));
}

// 初始化界面
void PeripheralsManagementWidget::initUI()
{
    // 主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(10, 20, 10, 10);
    mainLayout->setSpacing(10);

    // 标题
    QLabel *titleLabel = new QLabel(tr("Peripherals Management"), this);
    titleLabel->setContentsMargins(10, 0, 0, 0);
    QFont font1 = titleLabel->font();
    font1.setBold(true);
    titleLabel->setFont(font1);
//    DFontSizeManager::instance()->bind(titleLabel, DFontSizeManager::T5);
    mainLayout->addWidget(titleLabel);

    // USB接口
    m_USBSwitchItem = new SafetyProtectionItem(this);
    m_USBSwitchItem->setText(tr("USB port blocking"));
    m_USBSwitchItem->setFixedHeight(48);
//    DFontSizeManager::instance()->bind(m_USBSwitchItem->findChild<DLabel *>(), DFontSizeManager::T6);
    mainLayout->addWidget(m_USBSwitchItem);

    m_USBSwitchItem->setSwitchStatus(m_model->getPeripheralsKey(PERIPHERALS_MANAGE_USB_STATUS));
    connect(m_USBSwitchItem, SIGNAL(switchChanged(bool)), this, SLOT(acceptUSBSwitchItemChange(bool)));

    // 摄像头
    m_cameraSwitchItem = new SafetyProtectionItem(this);
    m_cameraSwitchItem->setText(tr("Camera blocking"));
    m_cameraSwitchItem->setFixedHeight(48);
//    DFontSizeManager::instance()->bind(m_cameraSwitchItem->findChild<DLabel *>(), DFontSizeManager::T6);
    mainLayout->addWidget(m_cameraSwitchItem);

    m_cameraSwitchItem->setSwitchStatus(m_model->getPeripheralsKey(PERIPHERALS_MANAGE_CAMERA_STATUS));
    connect(m_cameraSwitchItem, SIGNAL(switchChanged(bool)), this, SLOT(acceptCameraSwitchItemChange(bool)));

    // 打印机
    m_printerSwitchItem = new SafetyProtectionItem(this);
    m_printerSwitchItem->setText(tr("Printer blocking"));
    m_printerSwitchItem->setFixedHeight(48);
//    DFontSizeManager::instance()->bind(m_printerSwitchItem->findChild<DLabel *>(), DFontSizeManager::T6);
    mainLayout->addWidget(m_printerSwitchItem);

    m_printerSwitchItem->setSwitchStatus(m_model->getPeripheralsKey(PERIPHERALS_MANAGE_PRINTER_STATUS));
    connect(m_printerSwitchItem, SIGNAL(switchChanged(bool)), this, SLOT(acceptprinterSwitchItemChange(bool)));

    QSpacerItem *spacer = new QSpacerItem(20, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
    mainLayout->addItem(spacer);
}

// USB管控切换槽
void PeripheralsManagementWidget::acceptUSBSwitchItemChange(bool status)
{
    this->parentWidget()->parentWidget()->setEnabled(false);
    if (status) {
        // 屏蔽摄像头和打印机
        m_cameraSwitchItem->setVisible(false);
        m_printerSwitchItem->setVisible(false);

        // 开启询问弹框
        PeripheralsManageOpenDialog *m_peripheralsManageOpenDialog = new PeripheralsManageOpenDialog();
        m_peripheralsManageOpenDialog->setDialogContext(tr("If turned on, all USB-connected devices except keyboards and mouses will be disabled. Are you sure you want to turn it on?"));
//        connect(m_peripheralsManageOpenDialog, &PeripheralsManageOpenDialog::sendPeripheralsManageOpen, this, [=]() {
//            // 开启USB开关
//            m_model->setPeripheralsStatus(SettingType::USBManage, true);
//            m_model->setPeripheralsKey(PERIPHERALS_MANAGE_USB_STATUS, true);
//            m_model->setPeripheralsKey(PERIPHERALS_MANAGE_CAMERA_STATUS, true);
//            m_model->setPeripheralsKey(PERIPHERALS_MANAGE_PRINTER_STATUS, true);
//        });
//        connect(m_peripheralsManageOpenDialog, &PeripheralsManageOpenDialog::sendPeripheralsManageOpenFailed, this, [=]() {
//            m_USBSwitchItem->blockSignals(true);
//            m_USBSwitchItem->setSwitchStatus(false);
//            // 显示摄像头和打印机
//            m_cameraSwitchItem->setVisible(true);
//            m_printerSwitchItem->setVisible(true);
//            m_USBSwitchItem->blockSignals(false);
//        });
//        connect(m_peripheralsManageOpenDialog, &PeripheralsManageOpenDialog::onDialogClose, this, [&]() {
//            m_peripheralsManageOpenDialog = nullptr;
//        });
        m_peripheralsManageOpenDialog->setAttribute(Qt::WA_DeleteOnClose);
        m_peripheralsManageOpenDialog->setOnButtonClickedClose(true);
        m_peripheralsManageOpenDialog->show();
    } else {
        // 显示摄像头和打印机
        m_cameraSwitchItem->setVisible(true);
        m_printerSwitchItem->setVisible(true);

        // 关闭USB开关
        m_model->setPeripheralsStatus(SettingType::USBManage, false);
        m_model->setPeripheralsKey(PERIPHERALS_MANAGE_USB_STATUS, false);
        m_model->setPeripheralsKey(PERIPHERALS_MANAGE_CAMERA_STATUS, false);
        m_model->setPeripheralsKey(PERIPHERALS_MANAGE_PRINTER_STATUS, false);

        m_cameraSwitchItem->blockSignals(true);
        m_printerSwitchItem->blockSignals(true);
        m_cameraSwitchItem->setSwitchStatus(m_model->getPeripheralsKey(PERIPHERALS_MANAGE_CAMERA_STATUS));
        m_printerSwitchItem->setSwitchStatus(m_model->getPeripheralsKey(PERIPHERALS_MANAGE_PRINTER_STATUS));
        m_cameraSwitchItem->blockSignals(false);
        m_printerSwitchItem->blockSignals(false);

        DDialog dialog;
        dialog.setFixedSize(QSize(400, 200));
        dialog.setIcon(QIcon::fromTheme("deepin-defender"));
        dialog.setModal(true);
        dialog.setMessage(tr("USB port blocking has been turned off and relevant devices have been re-enabled. If there are any problems, please reconnect the device."));
        dialog.setWordWrapMessage(true);
        dialog.addButton(tr("OK", "Button"), false);

        dialog.exec();
    }

    this->parentWidget()->parentWidget()->setEnabled(true);
}
// 摄像头管控切换槽
void PeripheralsManagementWidget::acceptCameraSwitchItemChange(bool status)
{
    this->parentWidget()->parentWidget()->setEnabled(false);

    if (status) {
        // 开启询问弹框
        PeripheralsManageOpenDialog *m_peripheralsManageOpenDialog = new PeripheralsManageOpenDialog();
        m_peripheralsManageOpenDialog->setDialogContext(tr("If turned on, all USB-connected cameras will be disabled. Are you sure you want to turn it on?"));
//        connect(m_peripheralsManageOpenDialog, &PeripheralsManageOpenDialog::sendPeripheralsManageOpen, this, [=]() {
//            // 开启摄像头开关
//            m_model->setPeripheralsStatus(SettingType::CameraManage, true);
//            m_model->setPeripheralsKey(PERIPHERALS_MANAGE_CAMERA_STATUS, true);
//        });
//        connect(m_peripheralsManageOpenDialog, &PeripheralsManageOpenDialog::sendPeripheralsManageOpenFailed, this, [=]() {
//            m_cameraSwitchItem->blockSignals(true);
//            m_cameraSwitchItem->setSwitchStatus(false);
//            m_cameraSwitchItem->blockSignals(false);
//        });
//        connect(m_peripheralsManageOpenDialog, &PeripheralsManageOpenDialog::onDialogClose, this, [&]() {
//            m_peripheralsManageOpenDialog = nullptr;
//        });
        m_peripheralsManageOpenDialog->setAttribute(Qt::WA_DeleteOnClose);
        m_peripheralsManageOpenDialog->setOnButtonClickedClose(true);
        m_peripheralsManageOpenDialog->show();
    } else {
        // 关闭摄像头开关
        m_model->setPeripheralsStatus(SettingType::CameraManage, false);
        m_model->setPeripheralsKey(PERIPHERALS_MANAGE_CAMERA_STATUS, false);

        DDialog dialog;
        dialog.setFixedSize(QSize(400, 200));
        dialog.setIcon(QIcon::fromTheme("deepin-defender"));
        dialog.setModal(true);
        dialog.setMessage(tr("Camera blocking has been turned off and relevant devices have been re-enabled. If there are any problems, please reconnect the device."));
        dialog.setWordWrapMessage(true);
        dialog.addButton(tr("OK", "Button"), false);

        dialog.exec();
    }

    this->parentWidget()->parentWidget()->setEnabled(true);
}
// 打印机管控切换槽
void PeripheralsManagementWidget::acceptprinterSwitchItemChange(bool status)
{
    this->parentWidget()->parentWidget()->setEnabled(false);

    if (status) {
        // 开启询问弹框
        PeripheralsManageOpenDialog *m_peripheralsManageOpenDialog = new PeripheralsManageOpenDialog();
        m_peripheralsManageOpenDialog->setDialogContext(tr("If turned on, all USB-connected printers will be disabled. Are you sure you want to turn it on?"));
//        connect(m_peripheralsManageOpenDialog, &PeripheralsManageOpenDialog::sendPeripheralsManageOpen, this, [=]() {
//            // 开启摄像头开关
//            m_model->setPeripheralsStatus(SettingType::PrinterManage, true);
//            m_model->setPeripheralsKey(PERIPHERALS_MANAGE_PRINTER_STATUS, true);
//        });
//        connect(m_peripheralsManageOpenDialog, &PeripheralsManageOpenDialog::sendPeripheralsManageOpenFailed, this, [=]() {
//            m_printerSwitchItem->blockSignals(true);
//            m_printerSwitchItem->setSwitchStatus(false);
//            m_printerSwitchItem->blockSignals(false);
//        });
//        connect(m_peripheralsManageOpenDialog, &PeripheralsManageOpenDialog::onDialogClose, this, [&]() {
//            m_peripheralsManageOpenDialog = nullptr;
//        });
        m_peripheralsManageOpenDialog->setAttribute(Qt::WA_DeleteOnClose);
        m_peripheralsManageOpenDialog->setOnButtonClickedClose(true);
        m_peripheralsManageOpenDialog->show();
    } else {
        // 关闭摄像头开关
        m_model->setPeripheralsStatus(SettingType::PrinterManage, false);
        m_model->setPeripheralsKey(PERIPHERALS_MANAGE_PRINTER_STATUS, false);

        DDialog dialog;
        dialog.setFixedSize(QSize(400, 200));
        dialog.setIcon(QIcon::fromTheme("deepin-defender"));
        dialog.setModal(true);
        dialog.setMessage(tr("Printer blocking has been turned off and relevant devices have been re-enabled. If there are any problems, please reconnect the device."));
        dialog.setWordWrapMessage(true);
        dialog.addButton(tr("OK", "Button"), false);

        dialog.exec();
    }

    this->parentWidget()->parentWidget()->setEnabled(true);
}

void PeripheralsManagementWidget::notifyPeripheralsRevert()
{
    m_USBSwitchItem->setSwitchStatus(false);
}

void PeripheralsManagementWidget::synPeripheralsStatus(const QString &key, bool open)
{
    if (PERIPHERALS_MANAGE_USB_STATUS == key) {
        m_USBSwitchItem->blockSignals(true);
        m_USBSwitchItem->setSwitchStatus(open);
        m_USBSwitchItem->blockSignals(false);
    }

    if (PERIPHERALS_MANAGE_CAMERA_STATUS == key) {
        m_cameraSwitchItem->blockSignals(true);
        m_cameraSwitchItem->setSwitchStatus(open);
        m_cameraSwitchItem->blockSignals(false);
    }

    if (PERIPHERALS_MANAGE_PRINTER_STATUS == key) {
        m_printerSwitchItem->blockSignals(true);
        m_printerSwitchItem->setSwitchStatus(open);
        m_printerSwitchItem->blockSignals(false);
    }
}
